﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer;

namespace ZWZ.Web.WebAdmin
{
    public partial class um : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserList.Columns = new List<UCtrs.ListColumn>() { 
                    new UCtrs.ListColumn(){ Caption="姓名", FieldName="NickName", Width="100px"},
                    new UCtrs.ListColumn(){ Caption="用户组", FieldName="GroupName", Width="120px"},
                    new UCtrs.ListColumn(){ Caption="性别", FieldName="Gender", Width="40px"},
                    new UCtrs.ListColumn(){ Caption="电话", FieldName="Tel", Width="120px"},
                    new UCtrs.ListColumn(){ Caption="邮箱", FieldName="Email", Width="150px"},
                    new UCtrs.ListColumn(){ Caption="简介", FieldName="Introduction", Style="text-align:left;"},
                    new UCtrs.ListColumn(){ Caption="状态", FieldName="Status", Width="40px"}
                };
                BindData();
            }
        }
        protected void UserList_PageChanging(object src, PageChangingEventArgs e)
        {
            UserList.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }
        protected void BindData()
        {
            UserList.RecordCount = DBProvider.DB.Ctx.User_Info.Count();
            UserList.DataSource = DBProvider.DB.Ctx.User_Info.OrderBy(i => i.NickName).Skip((UserList.CurrentPageIndex - 1) * UserList.PageSize).Take(UserList.PageSize).ToList();
            UserList.DataBind();
        }
        protected void UserList_ItemCommand(object src, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string id = e.CommandArgument.ToString();
                var oldmodel = DBProvider.DB.Ctx.User_Info.Where(i => i.ID == id).FirstOrDefault();
                if (oldmodel != null)
                {
                    DBProvider.DB.Ctx.User_Info.DeleteObject(oldmodel);
                    DBProvider.DB.Ctx.SaveChanges();
                }
                BindData();
            }
        }
        protected void UserList_ItemDataBond(object src, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                //判断权限
                var user = e.Item.DataItem as ModelBase.User_Info;
                if (user.GroupType == ZWZ.Enums.User.GroupType.系统.ToString())
                {
                    (e.Item.FindControl("BtnUpdate") as ImageButton).Visible = false;
                    (e.Item.FindControl("BtnDelete") as ImageButton).Visible = false;
                }
            }
        }
    }
}